import os
import struct

import DataExtraction.BSAHeader

class BSAHeaderCFA(DataExtraction.BSAHeader.BSAHeader):
    '''Header for CFA resources'''
    
    def __init__(self, width_uncompressed = 0, height = 0, width_compressed = 0,
                 u2 = 0, u3 = 0, bits_per_pixel = 0, frame_count = 0, header_size = 0):
        DataExtraction.BSAHeader.BSAHeader.__init__(self, width_uncompressed, height)
        self._width_compressed = width_compressed
        self._u2 = u2
        self._u3 = u3
        self._bits_per_pixel = bits_per_pixel
        self._frame_count = frame_count
        self._header_size = header_size
        
        # 5 WORD's + 2 BYTE's + 1 WORD
        self._unpack_fmt = 5 * 'H' + 2 * 'B' + 'H'
        self._size_in_bytes = struct.calcsize(self._unpack_fmt)
        
    def __repr__(self):
        return "BSAHeaderCFA: [width_unc=" + str(self.get_width()) + "|height=" +\
            str(self.get_height()) + "|width_c=" + str(self._width_compressed) +\
            "|u2=" + str(self._u2) + "|u3=" + str(self._u3) +\
            "|bpp=" + str(self._bits_per_pixel) + "|frame_count=" + str(self._frame_count) +\
            "|header_size=" + str(self._header_size) + "]"
            
    # getters
    def get_width_uncompressed(self):
        return self.get_width()
    
    def get_width_compressed(self):
        return self._width_compressed
    
    def get_u2(self):
        return self._u2
    
    def get_u3(self):
        return self._u3
    
    def get_bits_per_pixel(self):
        return self._bits_per_pixel
    
    def get_frame_count(self):
        return self._frame_count
    
    def get_header_size(self):
        return self._header_size
    
    def get_size_in_bytes(self):
        return self._size_in_bytes
    
    # setters
    def set_width_uncompressed(self, width_uncompressed):
        self.set_width(width_uncompressed)
    
    def set_width_compressed(self, width_compressed):
        self._width_compressed = width_compressed
    
    def set_u2(self, u2):
        self._u2 = u2
    
    def set_u3(self, u3):
        self._u3 = u3
    
    def set_bits_per_pixel(self, bits_per_pixel):
        self._bits_per_pixel = bits_per_pixel
    
    def set_frame_count(self, frame_count):
        self._frame_count = frame_count
    
    def set_header_size(self, header_size):
        self._header_size = header_size
        
    def load(self, global_bsa_data, start):
        curr_offset = global_bsa_data.tell()
        
        global_bsa_data.seek(start, os.SEEK_SET)
        
        r = global_bsa_data.read(self._size_in_bytes)
        
        self._width, self._height, self._width_compressed, self._u2,\
            self._u3, self._bits_per_pixel, self._frame_count, self._header_size \
             = struct.unpack(self._unpack_fmt, r)
        
        global_bsa_data.seek(curr_offset, os.SEEK_SET)